package src.com.neusoft.nep.controller;

import src.com.neusoft.nep.common.AlertUtils;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.AdminUser;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class AdminUserLoginViewController {

    @FXML
    private TextField txtPhoneNumber;
    @FXML
    private PasswordField txtPassword;
    @FXML
    private Button btn_login;
    public void login() throws IOException {
        Map<String, AdminUser> userMap = FileUtils.readMapObject("adminUser.txt");
        String phoneNumber = txtPhoneNumber.getText();
        String password = txtPassword.getText();
        if (userMap.containsKey(phoneNumber)){
            AdminUser u = userMap.get(phoneNumber);
            if (password.equals(u.getPassword())){
                //登录成功跳转页面
                FXMLLoader fxmlLoader = new FXMLLoader();
                URL url = getClass().getResource("../view/AdminView.fxml");
                fxmlLoader.setLocation(url);
                Parent root = fxmlLoader.load();
                Scene scene = btn_login.getScene();
                Stage stage = (Stage) scene.getWindow();
                scene.setRoot(root);
                stage.show();
            }else {
                AlertUtils.showDialog("用户登录","密码不正确");
            }
        }else {
            AlertUtils.showDialog("用户登录","用户不存在");
        }
    }
    @FXML
    private Button btn_register;
    public void register() throws IOException{
        System.out.println("用户注册");
        Stage stage = (Stage) btn_register.getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/AdminUserRegisterView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_register.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private Button btn_back;
    @FXML
    public void back() {
        // 获取当前舞台
        Stage currentStage = (Stage) btn_back.getScene().getWindow();
        // 关闭当前舞台
        currentStage.close();
    }
}
